#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/pwm.h"
#include "motor-init.h"


#define A 65	//avanti
#define I 73 	//indietro
#define S 83	//sinistra
#define D 68	//destra
#define F 70	//fermo
//prova mercurial

//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif



//*****************************************************************************
//
// Variabili globali
//
//*****************************************************************************

uint32_t    ui32Data;

//*****************************************************************************
//
// 				Gestore dell'iterruzione sulla UART
//
//  n.b. per far funzionare gli interrupt occorre modificare tiva-startup.ccs
//*****************************************************************************
void
UARTIntHandler(void)
{
    uint32_t 	ui32Status;
    //
    // Get the interrrupt status.
    //
    ui32Status = UARTIntStatus(UART0_BASE, true);

    //
    // Clear the asserted interrupts.
    //
    UARTIntClear(UART0_BASE, ui32Status);

    //
    // Loop while there are characters in the receive FIFO.
    //
    while(UARTCharsAvail(UART0_BASE))
    {
        //
        // Read the next character from the UART and write it back to the UART.
        //
    	ui32Data=UARTCharGetNonBlocking(UART0_BASE);
        UARTCharPutNonBlocking(UART0_BASE,
        		ui32Data);


    }
}

//*****************************************************************************
//
// Send a string to the UART.
//
//*****************************************************************************
//void
//UARTSend(const uint8_t *pui8Buffer, uint32_t ui32Count)
//{
//    //
//    // Loop while there are more characters to send.
//    //
//    while(ui32Count--)
//    {
//        //
//        // Write the next character to the UART.
//        //
//        ROM_UARTCharPutNonBlocking(UART0_BASE, *pui8Buffer++);
//    }
//}


//*****************************************************************************
//
// Main
//
//*****************************************************************************
int
main(void)
{   //
    // Clock a 80MHz dal cristallo a 16MHz
    //
    SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);



	//Inizializzazioni
    console_init();
    int_init();
	pwm_init();



    //
    // Loop forever
    //
    while(1)
    {
    	switch(ui32Data)
    	{
    	case(A):
    			//avanti
    			pwm_power(50, 50);
    			pwm_dir(10, 10);
    			break;
    	case(D):
    			//destra
				pwm_power(50, 50);
    			pwm_dir(10, 20);
    			break;
    	case(S):
    			//sinistra
				pwm_power(50, 50);
    			pwm_dir (20, 10);
				break;
    	case(I):
    			//indietro
				pwm_power(50, 50);
    			pwm_dir (20, 20);
    			break;
    	case(F):
    			//indietro
    			pwm_power(0, 0);
    			pwm_dir (30, 30);
				break;
    	default:
    		break;
    	}
    }
}
